/**
 * @description: 任务传播
 *
 * @author majie
 * @date 2018年7月9日 下午21:00:52
 * @version 1.0.0.1
 */
var aemDiskTask = {

    accessToken : null ,
    exportType : null ,
    aemId : null ,

    init : function(accessToken,aemId,exportType){
    	aemDiskTask.accessToken = accessToken;
    	aemDiskTask.aemId = aemId;
    	aemDiskTask.exportType = exportType;
        return aemDiskTask;
    },
    //初始化统计分享数据
    initCount:function (){
    	 var data = {
    			 accessToken : aemDiskTask.accessToken,
    			 aemId : aemDiskTask.aemId
    	        };
	    var url = aemDiskTask.getPath() +"/aem/client/ajaxDiskTaskAllPoint";
	    //所有该用户的节点数据
	    var pointList;
	    var data = JSON.parse(aemDiskTask.syncAjax("post", url , data , false));   	
    	if(data.status=="200"){
    		pointList=data.data;
    	}
    	//计算分享数  首节点一定为该用户 
    	aemDiskTask.initAllCount(pointList);
    },
    //初始化所有散点数据
    initAllCount : function(data){
    	var initData = {
    			 accessToken : aemDiskTask.accessToken,
    			 aemId : aemDiskTask.aemId
    	        };
    	//图表数据
    	var webkitDep=null;	
    	//节点
    	var nodesData = new Array();
    	//节点之间连线
    	var linksData = new Array();
    	//获取节点的统计数据
    	var initPointUrl = aemDiskTask.getPath()+"/aem/client/ajaxDiskTaskAllPoint";
    	if(aemDiskTask.exportType==1){
    		initPointUrl = aemDiskTask.getPath()+"/aem/client/ajaxDiskTaskChildPoint";
    	}
    	var pointList = null;
    	var e = JSON.parse(aemDiskTask.syncAjax("post", initPointUrl , initData , false));  
    	if(e.status=="200"){
    		pointList=e.data;
    	}
    	//处理
    	if(pointList!=null){
    		var names = new Array();
    		for(var i=0;i<pointList.length;i++){
    			var nodes = new Object();
    			var links = new Object();
    			var point = pointList[i];
    			var openId = point.openId+"";
    			if(i==0){
    				nodes.symbolSize=20;
    				nodes.category=0;
    			}else{
    				nodes.symbolSize=10;
    				nodes.category=1;
    			}
    			if(i==0){
    				nodes.value=point;
    				nodes.name=openId;
    				nodesData.push(nodes);
    				names.push(openId);	
    			}else if(names.indexOf(openId)<0){			
    				nodes.value=point;
    				nodes.name=openId;
    				nodesData.push(nodes);
    				names.push(openId);	
    			}				
    			links.source=pointList[i].openId;
    			links.target=pointList[i].parentOpenId;
    			linksData[i]=links;
    		}
    	} 
    	//计算分享人数
    	 for(var i=0;i<nodesData.length;i++){
    		var shareCount = 0;
    		var nodes=nodesData[i].value;
    		var parentOpenId=nodes.openId;
    		//分享人数去重
    		var names = new Array();
    		for(var j=0;j<data.length;j++){
    			pointData=data[j];
    			if(aemDiskTask.StringEmpty(pointData.parentOpenId)&&pointData.parentOpenId==parentOpenId){
    				if(j==0){
        				names.push(pointData.openId);	
        				shareCount++;
        			}else if(names.indexOf(pointData.openId)<0){			
        				names.push(pointData.openId);	
        				shareCount++;
        			}
    			}
    		}
    		nodes.shareCount=shareCount;		
    	} 
    	webkitDep = {
    		    "type": "force",
    		    "categories": [//关系网类别，可以写多组
    		        {
    		            "name": "传播中心",//关系网名称
    		            "keyword": {},
    		            "itemStyle": {
    			                   "normal": {
    			                        "color": "#FF892F",
    			                   }
    			               }
    		        },{
    		            "name": "传播人",//关系网名称
    		            "keyword": {},
    		            "itemStyle": {
    			                   "normal": {
    			                        "color": "#FECE5A",
    			                   }
    			               }
    		        }
    		    ],
    		    "nodes": nodesData,
    		    "links":linksData 
    		};
    	aemDiskTask.initAllChart(webkitDep);
    	//初始化图表
    	/*if(aemDiskTask.exportType==0){
    		aemDiskTask.initAllChart(webkitDep);
    	}else{
    		aemDiskTask.initChildChart(webkitDep);
    		//初始化图表
    		 var temp = new initChildChart(webkitDep);
    	    temp.resize(); 
    	} */
    },
    //初始化所有传播节点
    initAllChart : function (webkitDep){
    	var allChart = echarts.init($(".all-chart")[0]);
    	allChart.showLoading();  
    	allChart.hideLoading();   
        allOption = {
                backgroundColor:'#686DA0',
                legend: {
                    show:false,
                    data: ['传播中心',"传播人"]//此处的数据必须和关系网类别中name相对应
                },
                  series: [{
                    type: 'graph',
                    layout: 'force',
                    animation: true,
                    nodeScaleRatio:0.6,
                     label: {
                    	 normal: {
                            show:true,
                            position: 'bottom',
                            borderRadius:10,
                            backgroundColor:'#fff',
                            borderWidth:20,
                            padding:5,
                            color:'#000',
                           	formatter: function (params, ticket, callback) {
                               	var param =params.value;
                               	var userName="神秘人";
                               	if(aemDiskTask.StringEmpty(param)){
                      	              if(aemDiskTask.StringEmpty(param.nickName)&&param.nickName!="null"){
                      	            	  userName = param.nickName;
                      	              }
                      	        }
                               	if(aemDiskTask.exportType==1&&aemDiskTask.StringEmpty(param)){
                    	            	  userName+="("+param.shareCount+")";
                    	        }
                               	return userName;
                               }
                    	 }
                    }, 
                    hoverAnimation:true,
                    focusNodeAdjacency:true,
                    draggable: true,  
                    roam : true,
                    data: webkitDep.nodes,  
                    categories: webkitDep.categories,  
                    force: {
                        edgeLength: 105,//连线的长度
                        repulsion: 100  //子节点之间的间距
                    },
                    edges: webkitDep.links
                }] 
            };
            allChart.setOption(allOption);
    },
    //初始化子节点
   /* initChildChart : function(webkitDep){
    	var childChart = echarts.init($(".child-chart")[0]);
    	childChart.showLoading();  
    	childChart.hideLoading();   
    	childOption = {
                backgroundColor:'#686DA0',
                legend: {
                    show:false,
                    data: ['传播中心',"传播人"]//此处的数据必须和关系网类别中name相对应
                },
                series: [{
                    type: 'graph',
                    layout: 'force',
                    animation: true,
                    nodeScaleRatio:0.6,
                     label: {
                    	 normal: {
                            show:true,
                            position: 'bottom',
                            borderRadius:10,
                            backgroundColor:'#fff',
                            borderWidth:20,
                            padding:5,
                            color:'#fff',
                           	formatter: function (params, ticket, callback) {
                               	var param =params.value;
                               	var userName="暂无";
                               	if(aemDiskTask.StringEmpty(param)){
                      	              if(aemDiskTask.StringEmpty(param.nickName)){
                      	            	  userName = param.nickName;
                      	              }
                      	             }
                               	var htmlStr = userName+"("+param.shareCount+")";
                               	return htmlStr;
                               }
                    	 }
                    }, 
                    draggable: true,  
                    data: webkitDep.nodes,  
                    categories: webkitDep.categories,  
                    force: {
                        edgeLength: 105,//连线的长度
                        repulsion: 100  //子节点之间的间距
                    },
                    edges: webkitDep.links
                }]
            };
    		childChart.setOption(childOption);
     		 $(function(){
    		        window.onresize = function(){
    		        	childChart.resize();
    		        }
    		    })
    		    this.resize = function size(){
    		        var childDiv=$
    		        var tabcontent = $(".point")[0];
    		        width=tabcontent.offsetWidth;
    		        height=tabcontent.offsetHeight;
    		        childDiv.style.width=width+'px';
    		        childDiv.style.height=height+'px';
    		        childChart.resize(width,height);
    		    } 
    },*/
    //判断字符串是否为空
    StringEmpty : function(content) {
        if (content == undefined || content == "" || content == null) {
            return false;
        } else {
            return true;
        }
    },

    // aemTask.getPath()
    getPath : function(){
        var parse = aemDiskTask.parseUrl(window.location.href);
        var path = parse.protocol + '://' + parse.host + ':' + parse.port;
        if("scrm_sps" == parse.segments[0]){
            path +=  '/' + parse.segments[0];
        }
        return path;
    },

    trim : function(str) {
        return str.replace(/(^\s+$)/g, "");
    },

    /**
     * 发送ajax请求
     *
     * type_ : post or get
     * url_ : url
     * data_ : data_
     * async_ : sync type ,true or false
     */
    syncAjax : function(type_, url_, data_  , async_) {
        var msg_ = null;
        $.ajax({
            dataType : "text",
            type : type_,
            url : url_,
            data : data_,
            async : async_,
            success : function(msg) {
                msg_ = msg;
            },
            error : function(msg) {
                msg_ = msg;
            }
        });
        return msg_;
    },

    parseUrl : function(url) {
        var a =  document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':',''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function(){
                var ret = {},
                    seg = a.search.replace(/^\?/,'').split('&'),
                    len = seg.length, i = 0, s;
                for (;i<len;i++) {
                    if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
            hash: a.hash.replace('#',''),
            path: a.pathname.replace(/^([^\/])/,'/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^\//,'').split('/')
        };
    }
}



















